# 9 类
# 类定义：
# 1. 编写__init__(self)方法
# 2. self类似于this，是指向实例本身的引用
class Dog:
    """对小狗简单的模拟"""
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def sit(self):
        print("%s is now sitting." % self.name)

    def roll_over(self):
        print("%s rolled over." % self.name)


# 创建类实例
my_dog = Dog("旺财", 3)
my_dog.sit()
my_dog.roll_over()
print("my dog is %s years old now." % my_dog.age)


# 9.3 继承
# 父类
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.yser = year
        self.odometer_reading = 0

    def get_description(self):
        print(f"{self.yser} {self.make} {self.model}".title())

    def read_odometer(self):
        print("This car has %s miles on it." % self.odometer_reading)


# 子类
class ElectricCar(Car):
    """电动汽车"""
    def __init__(self, make, model, year, battery):
        # 需要显式调用父类初始化
        super().__init__(make, model, year)
        self.battery = battery


class Battery:
    """电池"""
    def __init__(self):
        self.battery = 30

    def power(self):
        print("Remaining power %d%%" % self.battery)

    def charging(self):
        self.battery = 100


my_tesla = ElectricCar('tesla', 'model s', '2019', Battery())
my_tesla.get_description()
my_tesla.read_odometer()

my_tesla.battery.power()
my_tesla.battery.charging()
my_tesla.battery.power()

# 9.5 Python标准库
# https://docs.python.org/zh-cn/3/library/index.html
